/**

 @Name：核心模块
 @Author：GROW
    
 */

layui.define('view', function(exports) {
    var $ = layui.jquery,
        laytpl = layui.laytpl,
        element = layui.element,
        setter = layui.setter,
        view = layui.view,
        device = layui.device(),

        $win = $(window),
        $body = $('body'),
        container = $('#LAY_app'),

        SHOW = 'layui-show',
        HIDE = 'layui-hide',
        THIS = 'layui-this',
        DISABLED = 'layui-disabled',
        TEMP = 'template',
        APP_BODY = '#LAY_app_body',
        APP_FLEXIBLE = 'LAY_app_flexible',
        APP_SPREAD_SM = 'layadmin-side-spread-sm',
        TABS_BODY = 'layadmin-tabsbody-item',
        ICON_SHRINK = 'layui-icon-shrink-right',
        ICON_SPREAD = 'layui-icon-spread-left',
        SIDE_SHRINK = 'layadmin-side-shrink',
        SIDE_MENU = 'LAY-system-side-menu',

        //通用方法
        admin = {
            //数据的异步请求
            req: view.req,

            //并跳转到登入页
            exit: view.exit,

            userinfo: null,

            //xss 转义
            escape: function(html) {
                return String(html || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
                    .replace(/</g, '&lt;').replace(/>/g, '&gt;')
                    .replace(/'/g, '&#39;').replace(/"/g, '&quot;');
            },

            //事件监听
            on: function(events, callback) {
                return layui.onevent.call(this, setter.MOD_NAME, events, callback);
            },

            //弹出面板
            popup: view.popup,

            //右侧面板
            popupRight: function(options) {
                return admin.popup.index = layer.open($.extend({
                    type: 1,
                    id: 'LAY_adminPopupR',
                    anim: -1,
                    title: false,
                    closeBtn: false,
                    offset: 'r',
                    shade: 0.1,
                    shadeClose: true,
                    skin: 'layui-anim layui-anim-rl layui-layer-adminRight',
                    area: '300px'
                }, options));
            },

            //屏幕类型
            screen: function() {
                var width = $win.width();
                if (width > 1200) {
                    return 3; //大屏幕
                } else if (width > 992) {
                    return 2; //中屏幕
                } else if (width > 768) {
                    return 1; //小屏幕
                } else {
                    return 0; //超小屏幕
                }
            },

            //侧边伸缩
            sideFlexible: function(status) {
                var app = container,
                    iconElem = $('#' + APP_FLEXIBLE),
                    screen = admin.screen();

                //设置状态，PC：默认展开、移动：默认收缩
                if (status === 'spread') {
                    //切换到展开状态的 icon，箭头：←
                    iconElem.removeClass(ICON_SPREAD).addClass(ICON_SHRINK);

                    //移动：从左到右位移；PC：清除多余选择器恢复默认
                    if (screen < 2) {
                        app.addClass(APP_SPREAD_SM);
                    } else {
                        app.removeClass(APP_SPREAD_SM);
                    }

                    app.removeClass(SIDE_SHRINK)
                } else {
                    //切换到搜索状态的 icon，箭头：→
                    iconElem.removeClass(ICON_SHRINK).addClass(ICON_SPREAD);

                    //移动：清除多余选择器恢复默认；PC：从右往左收缩
                    if (screen < 2) {
                        app.removeClass(SIDE_SHRINK);
                    } else {
                        app.addClass(SIDE_SHRINK);
                    }

                    app.removeClass(APP_SPREAD_SM)
                }

                layui.event.call(this, setter.MOD_NAME, 'side({*})', {
                    status: status
                });
            },

            //resize事件管理
            resize: function(fn) {
                var router = layui.router(),
                    key = router.path.join('-');

                if (admin.resizeFn[key]) {
                    $win.off('resize', admin.resizeFn[key]);
                    delete admin.resizeFn[key];
                }

                if (fn === 'off') return; //如果是清除 resize 事件，则终止往下执行

                fn(), admin.resizeFn[key] = fn;
                $win.on('resize', admin.resizeFn[key]);
            },
            resizeFn: {},
            runResize: function() {
                var router = layui.router(),
                    key = router.path.join('-');
                admin.resizeFn[key] && admin.resizeFn[key]();
            },
            delResize: function() {
                this.resize('off');
            },

            //全屏
            fullScreen: function() {
                var ele = document.documentElement,
                    reqFullScreen = ele.requestFullScreen || ele.webkitRequestFullScreen ||
                    ele.mozRequestFullScreen || ele.msRequestFullscreen;
                if (typeof reqFullScreen !== 'undefined' && reqFullScreen) {
                    reqFullScreen.call(ele);
                };
            },

            //退出全屏
            exitScreen: function() {
                var ele = document.documentElement
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.webkitCancelFullScreen) {
                    document.webkitCancelFullScreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            },

            //纠正单页路由格式
            correctRouter: function(href) {
                if (!/^\//.test(href))
                    href = '/' + href;

                //纠正首尾
                return href.replace(/^(\/+)/, '/')
                    .replace(new RegExp('\/' + setter.entry + '$'), '/'); //过滤路由最后的默认视图文件名（如：index）
            }

            //……
        };

    //事件
    var events = admin.events = {
        //伸缩
        flexible: function(othis) {
            var iconElem = othis.find('#' + APP_FLEXIBLE),
                isSpread = iconElem.hasClass(ICON_SPREAD);
            admin.sideFlexible(isSpread ? 'spread' : null); //控制伸缩
        },

        //刷新
        refresh: function() {
            layui.index.render();
        },

        //全屏
        fullscreen: function(othis) {
            var SCREEN_FULL = 'layui-icon-screen-full',
                SCREEN_REST = 'layui-icon-screen-restore',
                iconElem = othis.children("i");

            if (iconElem.hasClass(SCREEN_FULL)) {
                admin.fullScreen();
                iconElem.addClass(SCREEN_REST).removeClass(SCREEN_FULL);
            } else {
                admin.exitScreen();
                iconElem.addClass(SCREEN_FULL).removeClass(SCREEN_REST);
            }
        },

        //返回上一页
        back: function() {
            history.back();
        },

        //遮罩
        shade: function() {
            admin.sideFlexible();
        }
    };

    //初始
    ! function() {

        //禁止水平滚动
        $body.addClass('layui-layout-body');

        container.addClass('layadmin-tabspage-none');

        //低版本IE提示
        if (device.ie && device.ie < 10) {
            view.error('IE' + device.ie + '下访问可能不佳，推荐使用：Chrome / Firefox / Edge 等高级浏览器', {
                offset: 'auto',
                id: 'LAY_errorIE'
            });
        }

    }();

    //监听 hash 改变侧边状态
    admin.on('hash(side)', function(router) {
        var path = router.path,
            getData = function(item) {
                return {
                    list: item.children('.layui-nav-child'),
                    name: item.data('name'),
                    jump: item.data('jump')
                }
            },
            sideMenu = $('#' + SIDE_MENU),
            SIDE_NAV_ITEMD = 'layui-nav-itemed',

            //捕获对应菜单
            matchMenu = function(list) {
                var pathURL = admin.correctRouter(path.join('/'));
                list.each(function(index1, item1) {
                    var othis1 = $(item1),
                        data1 = getData(othis1),
                        listChildren1 = data1.list.children('dd'),
                        matched1 = path[0] == data1.name || (index1 === 0 && !path[0]) ||
                        (data1.jump && pathURL == admin.correctRouter(data1.jump));

                    listChildren1.each(function(index2, item2) {
                        var othis2 = $(item2),
                            data2 = getData(othis2),
                            listChildren2 = data2.list.children('dd'),
                            matched2 = (path[0] == data1.name && path[1] == data2.name) ||
                            (data2.jump && pathURL == admin.correctRouter(data2.jump));

                        listChildren2.each(function(index3, item3) {
                            var othis3 = $(item3),
                                data3 = getData(othis3),
                                matched3 = (path[0] == data1.name && path[1] == data2.name && path[2] == data3.name) ||
                                (data3.jump && pathURL == admin.correctRouter(data3.jump))

                            if (matched3) {
                                var selected = data3.list[0] ? SIDE_NAV_ITEMD : THIS;
                                othis3.addClass(selected).siblings().removeClass(selected); //标记选择器
                                return false;
                            }

                        });

                        if (matched2) {
                            var selected = data2.list[0] ? SIDE_NAV_ITEMD : THIS;
                            othis2.addClass(selected).siblings().removeClass(selected); //标记选择器
                            return false
                        }

                    });

                    if (matched1) {
                        var selected = data1.list[0] ? SIDE_NAV_ITEMD : THIS;
                        othis1.addClass(selected).siblings().removeClass(selected); //标记选择器
                        return false;
                    }

                });
            };



        //重置状态
        sideMenu.find('.' + THIS).removeClass(THIS);

        //移动端点击菜单时自动收缩
        if (admin.screen() < 2) admin.sideFlexible();

        //开始捕获
        matchMenu(sideMenu.children('li'));
    });

    //监听侧边导航点击事件
    element.on('nav(layadmin-system-side-menu)', function(elem) {
        if (elem.siblings('.layui-nav-child')[0] && container.hasClass(SIDE_SHRINK)) {
            admin.sideFlexible('spread');
            layer.close(elem.data('index'));
        };
    });

    //页面跳转
    $body.on('click', '*[lay-href]', function() {
        var othis = $(this),
            href = othis.attr('lay-href'),
            router = layui.router();

        //执行跳转
        location.hash = admin.correctRouter(href);
    });

    //点击事件
    $body.on('click', '*[layadmin-event]', function() {
        var othis = $(this),
            attrEvent = othis.attr('layadmin-event');
        events[attrEvent] && events[attrEvent].call(this, othis);
    });

    //tips
    $body.on('mouseenter', '*[lay-tips]', function() {
        var othis = $(this);

        if (othis.parent().hasClass('layui-nav-item') && !container.hasClass(SIDE_SHRINK)) return;

        var tips = othis.attr('lay-tips'),
            offset = othis.attr('lay-offset'),
            direction = othis.attr('lay-direction'),
            index = layer.tips(tips, this, {
                tips: direction || 1,
                time: -1,
                success: function(layero, index) {
                    if (offset) {
                        layero.css('margin-left', offset + 'px');
                    }
                }
            });
        othis.data('index', index);
    }).on('mouseleave', '*[lay-tips]', function() {
        layer.close($(this).data('index'));
    });

    //窗口resize事件
    var resizeSystem = layui.data.resizeSystem = function() {
        //layer.close(events.note.index);
        layer.closeAll('tips');

        if (!resizeSystem.lock) {
            setTimeout(function() {
                admin.sideFlexible(admin.screen() < 2 ? '' : 'spread');
                delete resizeSystem.lock;
            }, 100);
        }

        resizeSystem.lock = true;
    }
    $win.on('resize', layui.data.resizeSystem);

    //接口输出
    exports('admin', admin);
});